<?php
class PictureController extends Controller {

    public $layout = '//layouts/column2';

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id),
        ));
    }

    public function actionCreate() {
        $model = new Picture;
        if (isset($_POST['Picture'])) {
            $model->attributes = $_POST['Picture'];
            $location = strrpos($_POST['Picture']['thumb'], '/') + 1;
            $model->source = '/uploads/source/800X800/' . substr($_POST['Picture']['thumb'], $location);
            if ($model->save())
                $this->redirect(array('view', 'id' => $model->id));
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id);
        if (isset($_POST['Picture'])) {
            $model->attributes = $_POST['Picture'];
            $location = strrpos($_POST['Picture']['thumb'], '/') + 1;
            $model->source = '/uploads/source/800X800/' . substr($_POST['Picture']['thumb'], $location);
            if ($model->save())
                $this->redirect(array('view', 'id' => $model->id));
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        if (Yii::app()->request->isPostRequest) {
            $this->loadModel($id)->delete();
            if (!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
        else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }

    public function actionIndex() {
        $dataProvider = new CActiveDataProvider('Picture', array(
            'criteria' => array(
                'with' => array('album'),
            ),
        ));
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }

    public function actionAdmin() {
        $model = new Picture('search');
        $model->unsetAttributes(); 
        if (isset($_GET['Picture']))
            $model->attributes = $_GET['Picture'];

        $this->render('admin', array(
            'model' => $model,
        ));
    }

    public function loadModel($id) {
        $model = Picture::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'picture-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

    public function actionUpload() {
        Yii::import('application.extensions.image.Image');
        $time = date("YmdHis", time());
        $file = CUploadedFile::getInstanceByName('file');
        $file->saveAs('uploads/tem/' . $file->getName());

        $sourceFile = Yii::app()->basePath . '/../uploads/tem/' . $file->getName();
        $picture    = $time . '_picture.' . substr($sourceFile, -3, 3);

        $simage     = new Image($sourceFile);
        $simage->resize(800, 800)->quality(100);

        $simage->save('uploads/source/800X800/' . $picture);

        $image     = new Image($sourceFile);
        $image->resize(400, 300)->quality(100);
        $image->save('uploads/thumb/400X300/' . $picture);
        echo Yii::app()->baseUrl . '/uploads/thumb/400X300/' . $picture;
        @unlink($sourceFile);
    }

}
